<?php

/**
 * Формы работы со справочниками
 */
class ReferencesForm extends Zend_Form {

    /**
     * Форма справочника
     *
     * @param int $id #ID Справочника
     * @return ReferencesForm
     */
    public function Reference($id = 0) {
        $this->setName('reference');
        $t = Zend_Registry::get('Zend_Locale');

        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $where = new Zend_Form_Element_Textarea('where');
        $where->setLabel('Where')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $order = new Zend_Form_Element_Textarea('order');
        $order->setLabel('Order')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $connection = new Zend_Form_Element_Checkbox('connection');
        $connection->setLabel('Section ID')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $param = new Zend_Form_Element_Text('param');
        $param->setLabel('Param')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $count = new Zend_Form_Element_Text('count');
        $count->setLabel('Count per page')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator('Digits');

        $breadcrumb = new Zend_Form_Element_Text('breadcrumb');
        $breadcrumb->setLabel('Breadcrumb')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $model = new ReferencesModel();
        $table = new Zend_Form_Element_Select('table');
        $array = $model->getTables();
        $table->addMultiOptions(array('' => 'Not defined') + $array)
                ->setLabel('Table')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $model = new FormsModel();
        $form = new Zend_Form_Element_Select('form');
        $array = $model->getFormsList();
        $form->addMultiOptions(array(0 => 'Not defined') + $array)
                ->setLabel('Form')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($title, $table, $form, $param, $count, $breadcrumb, $where, $order, $connection, $submit));
        $this->setAction(ADMIN_URL . '/references/edit/id/' . $id);
        $this->setDefaultTranslator($t);
        return $this;
    }

    /**
     * Форма выбора справочника
     *
     * @param int $sid #ID секции
     * @return ReferencesForm
     */
    public function pick($sid = 0) {
        $this->setName('reference');
        $t = Zend_Registry::get('Zend_Locale');
        $model = new ReferencesModel();
        $ref = new Zend_Form_Element_Select('rid');
        $array = $model->getRefsList();
        //print_r($array );exit;
        $empty = array(0 => $t->_('Undefined'));
        $ref->addMultiOptions($empty + $array)
                ->setLabel('Select reference')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($ref, $submit));
        $this->setAction(ADMIN_URL . '/pages/modify/id/' . $sid);
        $this->setDefaultTranslator($t);
        return $this;
    }

}
